<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="productId" prop="productid">
      <el-input v-model="dataForm.productid" placeholder="productId"></el-input>
    </el-form-item>
    <el-form-item label="苹果商品id" prop="appleProductid">
      <el-input v-model="dataForm.appleProductid" placeholder="苹果商品id"></el-input>
    </el-form-item>
    <el-form-item label="产品名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="产品名称"></el-input>
    </el-form-item>
    <el-form-item label="价格" prop="price">
      <el-input v-model="dataForm.price" placeholder="价格"></el-input>
    </el-form-item>
    <el-form-item label="展示价格" prop="showPrice">
      <el-input v-model="dataForm.showPrice" placeholder="展示价格"></el-input>
    </el-form-item>
    <el-form-item label="划线价格" prop="linePrice">
      <el-input v-model="dataForm.linePrice" placeholder="划线价格"></el-input>
    </el-form-item>
    <el-form-item label="每日价格" prop="dayPrice">
      <el-input v-model="dataForm.dayPrice" placeholder="每日价格"></el-input>
    </el-form-item>
    <el-form-item label="Vip增加天数" prop="vipDay">
      <el-input v-model="dataForm.vipDay" placeholder="Vip增加天数"></el-input>
    </el-form-item>
    <el-form-item label="所属分类id" prop="catagoryId">
      <el-input v-model="dataForm.catagoryId" placeholder="所属分类id"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          productid: '',
          appleProductid: '',
          name: '',
          price: '',
          showPrice: '',
          linePrice: '',
          dayPrice: '',
          vipDay: '',
          catagoryId: ''
        },
        dataRule: {
          productid: [
            { required: true, message: 'productId不能为空', trigger: 'blur' }
          ],
          appleProductid: [
            { required: true, message: '苹果商品id不能为空', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '产品名称不能为空', trigger: 'blur' }
          ],
          price: [
            { required: true, message: '价格不能为空', trigger: 'blur' }
          ],
          showPrice: [
            { required: true, message: '展示价格不能为空', trigger: 'blur' }
          ],
          linePrice: [
            { required: true, message: '划线价格不能为空', trigger: 'blur' }
          ],
          dayPrice: [
            { required: true, message: '每日价格不能为空', trigger: 'blur' }
          ],
          vipDay: [
            { required: true, message: 'Vip增加天数不能为空', trigger: 'blur' }
          ],
          catagoryId: [
            { required: true, message: '所属分类id不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/product/product/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.productid = data.product.productid
                this.dataForm.appleProductid = data.product.appleProductid
                this.dataForm.name = data.product.name
                this.dataForm.price = data.product.price
                this.dataForm.showPrice = data.product.showPrice
                this.dataForm.linePrice = data.product.linePrice
                this.dataForm.dayPrice = data.product.dayPrice
                this.dataForm.vipDay = data.product.vipDay
                this.dataForm.catagoryId = data.product.catagoryId
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/product/product/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'productid': this.dataForm.productid,
                'appleProductid': this.dataForm.appleProductid,
                'name': this.dataForm.name,
                'price': this.dataForm.price,
                'showPrice': this.dataForm.showPrice,
                'linePrice': this.dataForm.linePrice,
                'dayPrice': this.dataForm.dayPrice,
                'vipDay': this.dataForm.vipDay,
                'catagoryId': this.dataForm.catagoryId
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
